package cn.getech.data.development.controller;

import cn.getech.data.development.constant.DataDevelopmentBizExceptionEnum;
import cn.getech.data.development.constant.SelectRunStatus;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.model.dto.JobRunUdfDto;
import cn.getech.data.development.model.req.procinfo.ProcInfoListOwerReq;
import cn.getech.data.development.model.res.procinfo.ProcInfoListRes;
import cn.getech.data.development.model.vo.JobRunUdfVO;
import cn.getech.data.development.model.vo.ProcWithWorkMenusVO;
import cn.getech.data.development.model.vo.UdfJarPropertyVo;
import cn.getech.data.development.service.*;
import cn.getech.data.intelligence.common.exception.BizExceptionEnum;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.*;
import cn.getech.data.intelligence.common.validator.Assert;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.data.intelligence.common.validator.group.UpdateGroup;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.service.SysTenantUserService;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 项目操作
 *
 * @author zenith
 * @Date 2018-08-21 14:27:27
 */
@Api(tags = "项目操作")
@RestController
@RequestMapping("/bdp/procInfo")
public class ProcInfoController {

    @Autowired
    private ProcInfoService procInfoService;
    @Autowired
    private ProcUserService procUserService;

    @Autowired
    private JobInfoService jobInfoService;
    @Autowired
    private ProcConnectService procConnectService;
    @Autowired
    private ProcTableService procTableService;

    @Autowired
    private IAnalysisInfoService iAnalysisInfoService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RangerDataService rangerDataService;
    @Autowired
    private IBdpUdfJarInfoService bdpUdfJarInfoService;
    @Autowired
    private IWorkMenuService workMenuService;
    @Autowired
    private RealtimeQueueService realtimeQueueService;
    @Autowired
    private  WorkFlowLogService workFlowLogService;
    @Autowired
    private SysTenantUserService sysTenantUserService;



    @ApiOperation("获取可以看到的的项目")
    @GetMapping("/procs")
    public R procs(@RequestParam(name = "type") Integer type) {
        //如果是0：离线的 1：实时的
        List<ProcInfo> lists = procInfoService.procs(type);
        return R.okWithData(lists);
    }

    @ApiOperation("获取手动运行和定时运行的项目集合")
    @GetMapping("/logProcs")
    public R logProcs(@RequestParam(name = "type") Integer type) {
        //如果是0：手动 1：调度
        List<ProcInfo> lists = procInfoService.logProcs(type);
        return R.okWithData(lists);
    }


    @ApiOperation("获取项目关联的用户")
    @GetMapping("/listProcRelUsers/{procId}")
    //@SysLog(value="获取项目关联的用户",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:develop"})
    public R listProcRelUsers(@PathVariable("procId") Integer procId) {
        List<SysUserDto> allUserList = procUserService.listProcRelUsers(procId);
        return R.okWithData(allUserList);
    }


    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @PostMapping("/deleteProcByUserId/{userId}")
    @SysLog(value="删除项目",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R deleteProcByUserId(@PathVariable("userId")Long userId) {

        procUserService.deleteByUserId(userId);

        return R.ok();
    }

    /**
     * 获取列表
     */
    @ApiOperation("获取列表")
    @GetMapping("/list")
    //@SysLog(value="获取列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions(value={"bdp:dateExpore:list","bdp:procInfo:list"},logical=Logical.OR)
    public R list(@RequestParam Map<String, Object> params) {
        params.put("current_per", ShiroUtils.getUserId().intValue());
        params.put("sidx", "id");
        params.put("order", "desc");
        PageUtils page = procInfoService.selectListPage(params);
        List<ProcInfo> list = (List<ProcInfo>) page.getList();
        page.setList(list);
        return R.okWithPage(page);
    }

    @ApiOperation("获取可以看到的项目列表")
    @GetMapping("/seeList")
    public R seeList() {
        List<ProcInfo> list = procInfoService.seeList();
        return R.okWithData(list);
    }

    @ApiOperation("根据角色获取列表")
    @PostMapping("/listOwer")
    //@SysLog(value="根据角色获取列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
//    @RequiresPermissions(value={"bdp:procStatistics"},logical=Logical.OR)
    public PageRes<PageUtils2<ProcInfoListRes>> listOwer(@RequestBody ProcInfoListOwerReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils2<ProcInfoListRes> page = procInfoService.listOwer(req, ShiroUtils.getUserId());

        return BaseRes.okWithPage(page);
    }

    @ApiOperation("获取项目创建人信息列表")
    @GetMapping("/userList")
    //@SysLog(value="获取项目创建人信息列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R userList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = procInfoService.listUser(params);
        return R.okWithData(pageUtils);
    }

    @ApiOperation("获取项目和菜单列表")
    @GetMapping("/procMenus")
    //@SysLog(value="获取项目和菜单列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = ProcWithWorkMenusVO.class)})
    public R procMenus(Integer type) {
        return R.okWithData(workMenuService.procWithWorkMenus(type));
    }


    @ApiOperation("获取用户可以看到的项目")
    @GetMapping("/listUserProcs")
    //@SysLog(value="获取用户可以看到的项目",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R listUserProcs(){
        List<ProcInfo> list = procInfoService.listUserProcs(ShiroUtils.getUserId());
        return R.okWithData(list);
    }

    /**
     * 保存项目
     */
    @SysLog(value="新增项目",type= OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("保存项目")
    @PostMapping("/save")
    //todo 0925
    //@RequiresPermissions("bdp:procInfo:save")
    public R save(@RequestBody ProcInfo procInfo) {
        ValidatorUtils.validateEntity(procInfo);
        if (procInfo.getName().length() > 100) {
            throw new RRException("项目名称过长,请不要超过100个字符");
        }
        if (procInfo.getDesc().length() > 250) {
            throw new RRException("项目描述过长,请不要超过250个字符");
        }

        procInfo.setCreatePer(ShiroUtils.getUserId().intValue());
        procInfo.setCreateTime(DateUtil.date());
        procInfo.setModPer(procInfo.getCreatePer());
        procInfo.setModTime(procInfo.getCreateTime());
        procInfo.setTenantId(ShiroUtils.getTenantId());

        //验证队列是否存在
        realtimeQueueService.cheackQueue(procInfo.getQueueId());

        //保存项目
        procInfoService.save(procInfo);
        ProcInfo procInfo1=procInfoService.getOne(new QueryWrapper<ProcInfo>().eq("id",procInfo.getId()).eq("create_per",procInfo.getCreatePer()));
        ProcUser procUser=new ProcUser();
        procUser.setProcId(procInfo1.getId());
        procUser.setUserId(procInfo.getCreatePer());
        procUser.setIsOwner(1);
        procUserService.save(procUser);
        return R.ok();
    }

    /**
     * 删除项目
     */
    @SysLog(value="删除项目",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("删除项目")
    @PostMapping("/delete")
    //todo 0925
    //@RequiresPermissions("bdp:procInfo:delete")
    public R delete(@RequestBody ProcInfo param) {
        Integer id=param.getId();
        if (!procInfoService.checkExistById(param.getId())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_NOT_EXIST.getMessage());
        }
        if(procInfoService.checkIfRef(id)){
            throw new RRException(DataDevelopmentBizExceptionEnum.DEPENDED_EXIST.getMessage());
        }
        if(!procInfoService.checkPermission(param.getId())){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        if(procInfoService.removeById(id)){
            procUserService.remove(new QueryWrapper<ProcUser>().eq("proc_id",id));
        }

        return R.ok();
    }

    /**
     * 修改项目
     */

    @SysLog(value="修改项目",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("修改项目")
    @PostMapping("/update")
    //todo 0925
    //@RequiresPermissions("bdp:procInfo:update")
    public R update(@RequestBody ProcInfo param) {
        ValidatorUtils.validateEntity(param);
        ProcInfo info=procInfoService.getById(param.getId());
        if (!procInfoService.checkExistById(param.getId())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_NOT_EXIST.getMessage());
        }
        if(!procInfoService.checkPermission(param.getId())){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        if(param.getName().length()>100){
            throw new RRException(DataDevelopmentBizExceptionEnum.NAME_TOO_LONG.getMessage());
        }
        if(param.getDesc().length()>250){
            throw new RRException(DataDevelopmentBizExceptionEnum.DESC_TOO_LONG.getMessage());
        }
        //验证队列是否存在
        realtimeQueueService.cheackQueue(param.getQueueId());

        info.setName(param.getName());
        info.setModPer(ShiroUtils.getUserId().intValue());
        info.setModTime(DateUtil.date());
        info.setQueueId(param.getQueueId());
        info.setDesc(param.getDesc());
        try {
            if(procInfoService.updateById(info)){
                return R.ok();
            }else{
                return R.error(500,"修改失败");
            }
        } catch (Exception e) {
            throw new RRException(DataDevelopmentBizExceptionEnum.SERVER_ERROR.getMessage());
        }
    }


    /**
     * 项目详情
     */
    @ApiOperation("项目详情")
    @GetMapping("/info/{id}")
    @SysLog(value="项目详情",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:list"})
    public R info(@PathVariable("id") Integer id){
        if (!procInfoService.checkExistById(id)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_NOT_EXIST.getMessage());
        }
        if(!procUserService.checkViewPermission(id)){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        ProcInfo procInfo = procInfoService.getById(id);
        if(procInfo!=null) {
            if(null != procInfo.getQueueId()){
                RealtimeQueueEntity realtimeQueueEntity = realtimeQueueService.getById(procInfo.getQueueId());
                if(null != realtimeQueueEntity){
                    procInfo.setQueueName(realtimeQueueEntity.getQueueName());
                }
            }
            fillStat(procInfo);
        }
        return R.okWithData(procInfo);
    }

    /**todo 待优化
     * 获取项目的各个关联统计
     */
    public void fillStat(ProcInfo info){
        //添加创建用户的信息
        SysUserEntity sysUserEntity = sysUserService.getById(info.getCreatePer());
        info.setCreatePerName(null == sysUserEntity ? "" : sysUserEntity.getUsername());
        info.setConnects(procConnectService.count(new QueryWrapper<ProcConnect>().eq("proc_id",info.getId())));
        info.setTables(procTableService.count(new QueryWrapper<ProcTable>().eq("proc_id",info.getId())));
        info.setJobs(jobInfoService.count(new QueryWrapper<JobInfo>().eq("proc_id",info.getId())));
        info.setAnalysisCount(iAnalysisInfoService.count(new QueryWrapper<AnalysisInfo>().eq("proc_id",info.getId())));
    }


    /**
     * 保存项目管理
     */
    @SysLog(value="新增项目成员",type= OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("新增项目成员")
    @PostMapping("/saveProcUser")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R saveProcUser(@RequestBody ProcUser procUser){
        ValidatorUtils.validateEntity(procUser, AddGroup.class);
        if (!procInfoService.checkExistById(procUser.getProcId())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_NOT_EXIST.getMessage());
        }
        if(!procInfoService.checkPermission(procUser.getProcId())){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        SysUserEntity user=sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("username",procUser.getUserName()));
        if(null==user){
            throw new RRException(DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getMessage());
        }
        Integer user_id = user.getUserId().intValue();
        //判断是否是一个租户下的用户
        Integer count = sysTenantUserService.countData(user_id, ShiroUtils.getTenantId());
        if(null == count || count == 0){
            throw new RRException("当前租户下无此用户！");
        }
        procUser.setUserId(user_id);
        if(!procUserService.checkExistByProcUser(procUser)){
            //保存项目管理
            procUserService.save(procUser);

            //当前用户对应的策略
            //List<Long> userIds = Arrays.asList(user.getUserId());
            //rangerDataService.genteratorRangerPolicy(userIds);
            return R.ok();
        }
        else{
            throw new RRException(DataDevelopmentBizExceptionEnum.USER_EXIST.getMessage());
        }

    }

    /**
     * 删除项目
     */
    @SysLog(value="删除项目成员",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("删除项目成员")
    @PostMapping("/delProcUser")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R delProcUser(@RequestBody ProcUser procUser){
        ValidatorUtils.validateEntity(procUser, UpdateGroup.class);
        if(!procInfoService.checkPermission(procUser.getProcId())){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        if(procUserService.checkExistByProcUser(procUser)){

            //删除这些策略
            rangerDataService.deleteRangerPolicy(procUser);

            //保存项目管理
            procUserService.remove(new QueryWrapper<ProcUser>().eq("user_id",procUser.getUserId()).eq("proc_id",procUser.getProcId()));

//            rangerDataService.saveRangerPolicyROWFirlterByProcId(procUser.getProcId());

            rangerDataService.initAllHaveRowRulesTablePolicy();
            rangerDataService.initAllHaveMaskRulesTablePolicy();
        }
        return R.ok();
    }

    /**
     * 获取项目管理
     */
    //@SysLog(value="获取项目管理",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @ApiOperation("获取项目管理")
    @GetMapping("/getProcUser")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R getProcUser(@RequestParam Map<String, Object> params){
        if (null==params.get("proc_id")){
            throw new RRException("项目id不能为空");
        }
        if (params.get("limit")!=null&&params.get("limit").equals("")){
            throw new RRException("请传limit值");
        }
        if (params.get("page")!=null&&params.get("page").equals("")){
            throw new RRException("请传page值");
        }
        PageUtils procUserList=null;
        if(procInfoService.checkExistById(Integer.parseInt(params.get("proc_id").toString()))){
            //获取项目管理
            procUserList= procUserService.procUserList(params);
        }
        return R.okWithData(procUserList);
    }

    @ApiOperation("上传udf函数")
    @SysLog(value="上传udf函数",type= OperationTypeEnum.UPLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @PostMapping("/uploadJar")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R uploadJar(@RequestParam("procId") int procId,@RequestParam(value = "file") MultipartFile multipartFile){
//        UdfJarPropertyVo udfJarPropertyVo = new UdfJarPropertyVo();
        //仅限项目所属人操作
        if (!procInfoService.checkPermission(procId)){
            throw new RRException("没有权限上传jar包");
        }
        if (multipartFile == null) {
            return R.error("未检测到上传的jar包请您重新上传");
        }
        if(multipartFile.getOriginalFilename() == null){
            throw new RRException(BizExceptionEnum.MODEL_NAME_IS_NULL);
        }
        if(!multipartFile.getOriginalFilename().endsWith(".jar")){
            return R.error("请您上传jar格式文件");
        }
        if (multipartFile.isEmpty()) {
            throw new RRException("您上传的jar包为空!!!", 500);
        }

        try {
            //解析jar包类和方法
            Map<String,Boolean> result  = procInfoService.getJarDetials(multipartFile,procId);
            return R.okWithData(result);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("获取项目下的udf jar包")
    @GetMapping("/getUdfJars")
    //@SysLog(value="获取项目下的udf jar包",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R getUdfJars(@RequestParam Map<String,Object> params){
        Assert.isNull(params,"请求参数不能为空");
        if (null==params.get("procId")){
            throw new RRException("项目id不能为空");
        }
        //仅限项目所属人操作
        if (!procInfoService.checkPermission(Integer.parseInt(params.get("procId").toString()))){
            throw new RRException("没有权限查看该项目的jar包");
        }
        if (null!=params.get("limit")||null!=params.get("page")){
            PageUtils pageUtils = bdpUdfJarInfoService.selectJarsByProcAndName(params);
            return R.okWithData(pageUtils);
        }else{
            List<BdpUdfJarInfo> bdpUdfJarInfos = bdpUdfJarInfoService.list(new QueryWrapper<BdpUdfJarInfo>()
                    .eq("proc_id",params.get("procId"))
                    .like(null!=params.get("jarOriginName")&&StringUtils.isNotBlank(params.get("jarOriginName").toString()),"jar_origin_name",params.get("jarOriginName")));
            return R.okWithData(bdpUdfJarInfos);
        }
    }


    @ApiOperation("删除项目下的udf jar包")
    @SysLog(value="删除项目下的udf jar包",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @GetMapping("/deleteUdfJar/{id}")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R deleteUdfJar(@PathVariable(value = "id") Integer id){
        BdpUdfJarInfo bdpUdfJarInfo = bdpUdfJarInfoService.getById(id);
        if (null==bdpUdfJarInfo){
            throw new RRException("该jar包不存在");
        }
        //仅限项目所属人操作
        if (!procInfoService.checkPermission(bdpUdfJarInfo.getProcId())){
            throw new RRException("没有权限删除该项目的jar包");
        }
        List<JobRunUdf> jobRunUdfs = procInfoService.getProcUdfFuncs(bdpUdfJarInfo.getProcId(),bdpUdfJarInfo.getId());
        for (JobRunUdf jobRunUdf :jobRunUdfs){
            procInfoService.deleteUdfFunc(jobRunUdf.getId(),ShiroUtils.getUserId().intValue(),true);
        }
        procInfoService.deleteUdfJar(bdpUdfJarInfo);
        return R.ok();
    }


    @ApiOperation("注册udf")
    @SysLog(value="注册udf",type= OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @PostMapping("/generateUdf")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R generateUdf(@RequestBody UdfJarPropertyVo udfJarPropertyVo) throws SQLException {
        ValidatorUtils.validateEntity(udfJarPropertyVo, AddGroup.class);
        if (null==udfJarPropertyVo){
            throw new RRException("udf函数不能为空");
        }
        //仅限项目所属人操作
        if (!procInfoService.checkPermission(udfJarPropertyVo.getProcId())){
            throw new RRException("没有权限删除该项目的jar包");
        }
        if( udfJarPropertyVo.getJarId() == null){
            return R.error("上传数据缺失");
        }
        BdpUdfJarInfo bdpUdfJarInfo=bdpUdfJarInfoService.getById(udfJarPropertyVo.getJarId());
        if (null==bdpUdfJarInfo){
            throw new RRException("所选jar包不存在");
        }
        if (!bdpUdfJarInfo.getProcId().equals(udfJarPropertyVo.getProcId())){
            throw new RRException("所选jar包不属于该项目");
        }
        if (udfJarPropertyVo.getJarMethodName().length()>50){
            throw new RRException("udf函数名称不能超过50字");
        }
        procInfoService.registUdf(udfJarPropertyVo,bdpUdfJarInfo);
        return R.ok();
    }


    @ApiOperation("分页获取项目下的udf")
    //@SysLog(value="分页获取项目下的udf",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @GetMapping("/getUdfFuncs")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R getUdfFuncs(@RequestParam Map<String,Object> params){
        PageUtils procUdfFuncs = procInfoService.getProcUdfFuncsByPage(params);
        return R.okWithData(procUdfFuncs);
    }


    @ApiOperation("获取所有的udf")
    //@SysLog(value="获取所有的udf",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @GetMapping("/listUdfFuncs")
    public R listUdfFuncs(){
        List<JobRunUdfDto> jobRunUdfDtos = procInfoService.listUdfFuncs();
        return R.okWithData(PojoUtils.listConvert(JobRunUdfVO.class,jobRunUdfDtos));
    }


    @ApiOperation("删除项目下的udf")
    @SysLog(value="删除项目下的udf",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    @GetMapping("/deleteUdfFuncs/{id}")
    //todo 0925
    //@RequiresPermissions({"bdp:procInfo:manage"})
    public R deleteUdfFunc(@PathVariable(value = "id") Integer id){
        procInfoService.deleteUdfFunc(id,ShiroUtils.getUserId().intValue(),true);
        return R.ok();
    }


    /**
     * 运维中心-离线任务-项目下拉框列表
     */
    @ApiOperation("运维中心-离线任务-项目下拉框列表")
    @GetMapping("/logProjectList")
    //@SysLog(value="运维中心-离线任务-项目下拉框列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R list() {

        List<ProcInfo> list = procInfoService.getAllProcList();
        return R.okWithData(list);
    }

    /**
     * 运维中心-离线任务-调度队列下拉框列表
     */
    @ApiOperation("运维中心-离线任务-调度队列下拉框列表")
    @GetMapping("/scheduleQueueList")
    @SysLog(value="运维中心-离线任务-调度队列下拉框列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R scheduleQueuelist() {
        List<String> list=workFlowLogService.getLogQueueList();
        return R.okWithData(list);
    }


    /**
     * 运维中心-离线任务-状态下拉框列表
     */
    @ApiOperation("运维中心-离线任务-状态下拉框列表")
    @GetMapping("/stateList")
    @SysLog(value="运维中心-离线任务-状态下拉框列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R statusList() {
       //运维中心-离线任务列表-获取状态下拉框
        List<Map<String,Object>> stateList=new ArrayList<>();
        for(SelectRunStatus selectRunStatus:SelectRunStatus.values()){
            Map<String,Object>  map=new HashMap<>();
            map.put("state",selectRunStatus.getStatus());
            map.put("name",selectRunStatus.getDesc());
            stateList.add(map);
        }
        return R.okWithData(stateList);
    }


    /***
     * 初始化关联项目的的租户对应的数据源和离线表的权限
     * @return
     */
    @GetMapping("/initProcConnectsAndTables")
    public R initProcConnectsAndTables() {
        procInfoService.initProcConnectsAndTables();
        return R.ok();
    }


}
